package com.huadao.cart_ccs.experiment.addViruses;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ExperimentStageManager;
import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.common.dialog.IDialogResultListener;
import com.huadao.cart_ccs.experiment.addCell.AddCellDF;
import com.huadao.cart_ccs.greendao.entity.AddViruses;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class AddVirusesDF extends BaseDialogFragment implements IAddVirusesContract.IAddVirusesView {
    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.sp_type)
    Spinner spType;
    @BindView(R.id.et_virusesVolume)
    EditText etVirusesVolume;
    @BindView(R.id.tv_operate_date)
    TextView tvOperateDate;
    @BindView(R.id.tv_operate_time)
    TextView tvOperateTime;
    @BindView(R.id.tv_operate_people)
    TextView tvOperatePeople;
    @BindView(R.id.tv_history_record)
    TextView tvHistoryRecord;
    @BindView(R.id.btn_startAdd)
    Button btnStartAdd;
    @BindView(R.id.btn_finishInject)
    Button btnFinishInject;

    private AddVirusesPresenter presenter;

    private Date startDate;
    private Date endDate;
    private String operator;

    private OnAddViruses onAddViruses;

    public interface OnAddViruses{
        //开始添加
        void startAddViruses(int amount,String operator,Date startDate);
        //冲刷
        void scourAfterAddViruses();
    }
    @Override
    protected BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_add_viruses;
    }

    @Override
    protected void onInit() {
        presenter=new AddVirusesPresenter(this);
        operator=CartApplication.getInstance().getOperator();
        tvOperatePeople.setText(operator);
        presenter.getAddVirusesHistory();
        getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @Override
    public void onGetAddVirusesHistory(List<AddViruses> addVirusesList) {
        if (addVirusesList == null || addVirusesList.size() == 0) {
            tvHistoryRecord.setVisibility(View.GONE);
            return;
        }
        for (AddViruses injection : addVirusesList) {
            String text = String.format("登陆者于%tF %tT注入了体积为%dml的病毒", injection.getStartDate(), injection.getStartDate(), injection.getVirusesVolume());
            tvHistoryRecord.setVisibility(View.VISIBLE);
            tvHistoryRecord.append(text);
            tvHistoryRecord.append("\n");
        }
        initData(addVirusesList.get(0));
    }

    private void initData(AddViruses addViruses) {
        int index=0;
        String[] type=getResources().getStringArray(R.array.addCellType);
        for(int i=0;i<type.length;i++){
            if(addViruses.getType().equals(type[i])){
                index=i;
                break;
            }
        }
        spType.setSelection(index);
        etVirusesVolume.setText(String.valueOf(addViruses.getVirusesVolume()));
    }

    @OnClick({R.id.btn_close, R.id.btn_startAdd,R.id.btn_finishInject})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_startAdd:
                String err=checkParams();
                if(err!=null){
                    showToast(err);
                    return;
                }
                DialogFragmentHelper.showConfirmDialog(getActivity(), "确认添加", "请确认病毒悬液袋的卡纸是否开启！系统将开启进液泵，进行添加病毒！", new IDialogResultListener<Integer>() {
                    @Override
                    public void onDataResult(Integer result) {
                        if (result == DialogInterface.BUTTON_POSITIVE) {
                            startDate=new Date();
                            if(onAddViruses!=null){
                                int amount=Integer.parseInt(etVirusesVolume.getText().toString());
                                onAddViruses.startAddViruses(amount,operator,startDate);
                            }
                        }
                    }
                }, true);
                break;
            case R.id.btn_finishInject:
                endDate=new Date();
                //操作时间显示结束时间
                tvOperateDate.setText(TimeUtil.date_sdf.format(endDate));
                tvOperateTime.setText(TimeUtil.time_sdf.format(endDate));
                DialogFragmentHelper.showConfirmDialog(getActivity(), "确认冲刷", "完成添加病毒进入到冲刷阶段，请确认病毒悬液袋的卡纸是否关闭！系统将开启进液泵，用培养液进行冲刷！", new IDialogResultListener<Integer>() {
                    @Override
                    public void onDataResult(Integer result) {
                        if (result == DialogInterface.BUTTON_POSITIVE) {
                            AddVirusesDF.this.dismiss();
                            if(onAddViruses!=null){
                                AddViruses addViruses=generateAddViruses();
                                presenter.saveAddViruses(addViruses);
                                onAddViruses.scourAfterAddViruses();
                                AddVirusesDF.this.dismiss();
                            }
                        }
                    }
                }, true);
                break;
        }
    }

    private AddViruses generateAddViruses() {
        AddViruses addViruses=new AddViruses();
        String type= (String) spType.getSelectedItem();
        int virusesVolume=Integer.parseInt(etVirusesVolume.getText().toString());
        addViruses.setType(type);
        addViruses.setVirusesVolume(virusesVolume);
        addViruses.setStartDate(startDate);
        addViruses.setFinishDate(endDate);
        addViruses.setOperator(operator);
        addViruses.setProjectId(ProjectManager.getInstance().getProjectId());
        addViruses.setStageId(ExperimentStageManager.getInstance().getStageId());
        return addViruses;
    }

    private String checkParams() {
        String err = null;
        if (TextUtils.isEmpty(spType.getSelectedItem().toString())) {
            err = "请输入进样类型";
        }else if (TextUtils.isEmpty(etVirusesVolume.getText())) {
            err = "请输入病毒体积";
        }
        return err;
    }

    public void startAddSucc(){
        btnStartAdd.setText("正在添加病毒...");
        btnStartAdd.setBackgroundResource(R.drawable.btn_info_bg);
        btnStartAdd.setEnabled(false);
    }

    public void startAddFailed(){
        btnStartAdd.setText("重新添加病毒");
        btnStartAdd.setBackgroundResource(R.drawable.shape_dialog_positive_button);
        btnStartAdd.setEnabled(true);
    }

    public void finishAddViruses(){
        btnStartAdd.setVisibility(View.GONE);
        btnFinishInject.setVisibility(View.VISIBLE);
    }

    public void setOnAddViruses(OnAddViruses onAddViruses) {
        this.onAddViruses = onAddViruses;
    }
}
